//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

//====================================================
// Created     : 2004-1-3
// Author      : Fan Dian
// Description : Read values of some registers
//====================================================

#ifndef __ELASTOS_KTESTF_REGISTERS_H__
#define __ELASTOS_KTESTF_REGISTERS_H__

#include <core.h>
#include <../core/inc/_hal/x86/segment.h>

//====================================================
// Description  : These functions retrieve the value of control registers
//====================================================

inline UInt32 GetCr0()
{
    register UInt32 r;

    ASM (
        "movl %%cr0, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetCr3()
{
    register UInt32 r;

    ASM (
        "movl %%cr3, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetCr4()
{
    register UInt32 r;

    ASM (
        "movl %%cr4, %0;\n"
        : "=a" (r)
    );

    return r;
}

//====================================================
// Description  : These functions retrieves the value of segment registers
//====================================================

inline GdtPointer GetGdt()
{
    GdtPointer gdtPointer;

    ASM (
        "sgdt %0;\n"
        : "=m" (gdtPointer)
    );

    return gdtPointer;
}

inline IdtPointer GetIdt()
{
    IdtPointer idtPointer;

    ASM (
        "sidt %0;\n"
        : "=m" (idtPointer)
    );

    return idtPointer;
}

inline UInt32 GetLdt()
{
    register UInt32 r;

    ASM (
        "sldt %0;\n"
        : "=a" (r)
    );

    return r;
}

//====================================================
// Description  : These functions retrieves the value of segment selector
//                    registers
//====================================================

inline UInt32 GetCs()
{
    register UInt32 r;

    ASM (
        "movl %%cs, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetDs()
{
    register UInt32 r;

    ASM (
        "movl %%ds, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetEs()
{
    register UInt32 r;

    ASM (
        "movl %%es, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetFs()
{
    register UInt32 r;

    ASM (
        "movl %%fs, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetGs()
{
    register UInt32 r;

    ASM (
        "movl %%gs, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetSs()
{
    register UInt32 r;

    ASM (
        "movl %%ss, %0;\n"
        : "=a" (r)
    );

    return r;
}

inline UInt32 GetTss()
{
    register UInt32 r;

    ASM (
        "str %0;\n"
        : "=a" (r)
    );

    return r;
}

#endif // #ifndef __ELASTOS_KTESTF_REGISTERS_H__
